'use strict'
const AV = require('leanengine');
const qiniu = require('qiniu');

const accessKey = 'wVLlAeJ6KJm4TE1XrJ6fDnObPBn3JL_V5r3LtTnd';
const secretKey = 'ynOwkf9fNd36DonmyWxGt8IK5W6NIgc9cznQVkgo';
const mac = new qiniu.auth.digest.Mac(accessKey, secretKey);


module.exports = function() {
	AV.Cloud.define('uploadToken', (req)=>{
		let user = req.currentUser;
		if (!user)
			return {flag:0, msg : "please login."};
		console.log(req.params);
		let options = {
		  	scope: "shareshop",
		  	returnBody: '{"key":"$(key)","hash":"$(etag)","fsize":$(fsize),"bucket":"$(bucket)","name":"$(x:name)"}'
		}
		let putPolicy = new qiniu.rs.PutPolicy(options);
		let uploadToken=putPolicy.uploadToken(mac);
		return {flag : 1, token : uploadToken};
	});

	AV.Cloud.define('saveUploadImage', (req)=>{
		let user = req.currentUser;
		if (!user)
			return {flag:0, msg : "please login."};
		console.log(req.params);
		let imgUrl 	= req.params.url;
		let fsize 	= req.params.fsize;
		let userId 	= user.id;
		let key 	= req.params.key;
		let provider = "qiniu";
		let bucket 	= req.params.bucket;

		var ushObj = AV.Object.extend("_File");
		return {flag:1};
	})
}